﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Axis.Dao.News;
using Axis.BusinessObject.News;
using System.Web.Script.Serialization;
using Axis.BusinessObject.Base;

namespace Axis.Web.FrontEnd.Views.News
{
    [Serializable]
    public class CNewItems : CObjectBase
    {
        public CNewItems()
        {
            //CSVFields = new string[] { "ID", "Title", "Summary", "Content", "CreatedDateTime", "CreatedBy", "LastUpdatedDateTime", "LastUpdatedBy" };
        }
        #region Public property

        public int ID { get; set; }
        public string Code { get; set; }
        public string Title { get; set; }
        public string Summary { get; set; }
        public DateTime WritenDate { get; set; }
        public string Image { get; set; }
        public string ThumbnailImage { get; set; }
        public string Content { get; set; }
        public int ProjectVoteCount { get; set; }
        public decimal ProjectVotePercent { get; set; }
        #endregion
    }
    public class CNewUI
    {
        public string GetNewList(string CategoryID)
        {
            string Result = "";
            CNewsDao newDao = new CNewsDao();
            IList<CNews> list = newDao.GetNews(string.Format("<InputValue CategoryID='{0}' Context='Frontend'/>", CategoryID));
            foreach (CNews item in list)
                Result += GetNewsItem(item);
            return Result;
        }
        private string GetNewsList(string pInputValue)
        {
            CNewsDao newDao = new CNewsDao();
            IList<CNews> list = newDao.GetNews(pInputValue);
            IList<CNewItems> listNewItems = new List<CNewItems>();
            foreach (CNews item in list)
            {
                CNewItems temp = new CNewItems();
                temp.ID = item.ID;
                temp.Code = item.Code;
                temp.Title = item.Title;
                temp.Summary = item.Summary;
                temp.Image = item.Image;
                temp.ThumbnailImage = item.ThumbnailImage;
                temp.WritenDate = item.WritenDate;
                listNewItems.Add(temp);
            }

            JavaScriptSerializer js = new JavaScriptSerializer();
            string listSerializer = js.Serialize(listNewItems);
            int totalRecords = newDao.LastOutputValue.TotalRows;
            int totalPages = newDao.LastOutputValue.TotalPages;
            return string.Format("{{list:{0},total:{1},page:{2}}}", listSerializer, totalRecords, totalPages);
        }
        public string GetNewListByHome(string PageIndex, string RowsPerPage)
        {
            string inputValue = string.Format("<InputValue Context='HOME' RowsPerPage='{0}' PageIndex='{1}' sidx='' sord='desc'/>", RowsPerPage, PageIndex);
            return GetNewsList(inputValue);
        }
        public string GetNewList(string CategoryID, string PageIndex, string RowsPerPage)
        {

            string inputValue = string.Format("<InputValue CategoryID='{0}' Context='Frontend' RowsPerPage='{1}' PageIndex='{2}' sidx='' sord='desc'/>", CategoryID, RowsPerPage, PageIndex);
            return GetNewsList(inputValue);
        }
        public string GetTop1(string CategoryID)
        {
            CNewsDao newDao = new CNewsDao();
            IList<CNews> list = newDao.GetNews(string.Format("<InputValue CategoryID='{0}' Context='FRONTEND' Special='1' RowsPerPage='1' PageIndex='1' />", CategoryID));
            IList<CNewItems> listNewItems = new List<CNewItems>();
            foreach (CNews item in list)
            {
                CNewItems temp = new CNewItems();
                temp.ID = item.ID;
                temp.Code = item.Code;
                temp.Title = item.Title;
                temp.Summary = item.Summary;
                temp.Image = item.Image;
                temp.WritenDate = item.WritenDate;
                temp.Image = item.Image;
                temp.ThumbnailImage = item.ThumbnailImage;
                temp.Content = item.Content;
                temp.ProjectVoteCount = item.ProjectVoteCount;
                temp.ProjectVotePercent = item.ProjectVotePercent;
                listNewItems.Add(temp);
            }
            JavaScriptSerializer js = new JavaScriptSerializer();
            return js.Serialize(listNewItems);
        }

        public string GetNewsByTagID(string Tags, string PageIndex, string RowsPerPage)
        {
            CNewsDao newDao = new CNewsDao();
            IList<CNews> list = newDao.GetNews(string.Format("<InputValue Tags='{0}' Context='Frontend' RowsPerPage='{1}' PageIndex='{2}' sidx='' sord='desc'/>", Tags, RowsPerPage, PageIndex));
            IList<CNewItems> listNewItems = new List<CNewItems>();
            foreach (CNews item in list)
            {
                CNewItems temp = new CNewItems();
                temp.ID = item.ID;
                temp.Code = item.Code;
                temp.Title = item.Title;
                temp.Summary = item.Summary;
                temp.Image = item.Image;
                temp.ThumbnailImage = item.ThumbnailImage;
                temp.WritenDate = item.WritenDate;
                listNewItems.Add(temp);
            }

            JavaScriptSerializer js = new JavaScriptSerializer();
            string listSerializer = js.Serialize(listNewItems);
            int totalRecords = newDao.LastOutputValue.TotalRows;
            int totalPages = newDao.LastOutputValue.TotalPages;
            return string.Format("{{list:{0},total:{1},page:{2}}}", listSerializer, totalRecords, totalPages);
        }

        public string GetTop10(string CategoryID)
        {
            CNewsDao newDao = new CNewsDao();
            IList<CNews> list = newDao.GetNews(string.Format("<InputValue CategoryID='{0}' Context='TOP10' />", CategoryID));
            IList<CNewItems> listNewItems = new List<CNewItems>();
            foreach (CNews item in list)
            {
                CNewItems temp = new CNewItems();
                temp.ID = item.ID;
                temp.Code = item.Code;
                temp.Title = item.Title;
                temp.Summary = item.Summary;
                temp.Image = item.Image;
                temp.WritenDate = item.WritenDate;
                temp.Image = item.Image;
                temp.ThumbnailImage = item.ThumbnailImage;
                temp.ProjectVoteCount = item.ProjectVoteCount;
                temp.ProjectVotePercent = item.ProjectVotePercent;
                listNewItems.Add(temp);
            }
            JavaScriptSerializer js = new JavaScriptSerializer();
            return js.Serialize(listNewItems);
        }
        public string GetNewsItem(CNews Item)
        {
            string pattern = @"<li>"
        + "<div class='newsImage'><img class='imgNews' src='{0}' /></div>"
        + "<div class='newsPanel'>"
            + "<div>"
                + "<a href='javascript:ShowDetail({4});' class='newsTitle'>{1}</a>"
            + "</div>"
            + "<div class='newsDateTime'>(Cập nhật {2})</div>"
            + "<div class='newsDescription'>{3}....</div>"
            + "<div><a href='javascript:ShowDetail({4});' class='newsMore'>Chi tiết</a></div>"
        + "</div>"
    + "</li>";
            return string.Format(pattern, Item.Image, Item.Title, Item.WritenDate, Item.Summary, Item.ID);
        }

        public CNews GetNewDetail(string ID)
        {
            CNewsDao newDao = new CNewsDao();
            IList<CNews> list = newDao.GetNews(string.Format("<InputValue ID='{0}' Context='NEWSDETAIL'/>", ID));
            if (list != null)
                if (list.Count > 0)
                    return list[0];
            return null;
        }
        public string GetNewStatitic(string ID)
        {
            CNewsDao newDao = new CNewsDao();
            IList<CNews> list = newDao.GetNews(string.Format("<InputValue ID='{0}' Context='Statitic'/>", ID));
            IList<CNews> listNewItems = new List<CNews>();
            if (list != null)
                if (list.Count > 0)
                    listNewItems.Add(list[0]);
            JavaScriptSerializer js = new JavaScriptSerializer();
            return js.Serialize(listNewItems);         
        }
        public string GetNewsDetail2(string ID)
        {
            CNewsDao newDao = new CNewsDao();
            IList<CNews> list = newDao.GetNews(string.Format("<InputValue ID='{0}' Context='NEWSDETAIL'/>", ID));
            IList<CNewItems> listNewItems = new List<CNewItems>();
            foreach (CNews item in list)
            {
                CNewItems temp = new CNewItems();
                temp.Content = item.Content;
                listNewItems.Add(temp);
            }
            JavaScriptSerializer js = new JavaScriptSerializer();
            return js.Serialize(listNewItems);
        }
        public string GetNewsRelation(string ID)
        {
            CNewsDao newDao = new CNewsDao();
            IList<CNews> list = newDao.GetNews(string.Format("<InputValue ID='{0}' Context='REFERENCE'/>", ID));

            JavaScriptSerializer js = new JavaScriptSerializer();
            return js.Serialize(list);
        }
    }
}